﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace homeIMSClient
{
    public partial class frmAsset : Form
    {
        private Model.user _user;
        public homeIMSRef.IhomeIMSService service = new homeIMSRef.IhomeIMSServiceClient();
        //constructor for New Asset
        public frmAsset(Model.user User)
        {
            InitializeComponent();
            //set _user
            _user = User;

            //set text for New Asset
            this.Text = "New Asset - " + _user.UserName.ToString();
        }

        //overloaded constructor for filling form upon creation
        public frmAsset(Model.user User, Model.asset selectedAsset)
        {
            InitializeComponent();
            //set _user
            _user = User;

            //set text for existing asset
            this.Text = selectedAsset.Name.ToString() + " - " + _user.UserName.ToString();

            //send through hydrateForm()
            hydrateForm(selectedAsset);
        }

        //method will fill form with existing assets
        private void hydrateForm(Model.asset asset)
        {
            //set cursor
            Cursor.Current = Cursors.WaitCursor;

            //fill combo boxes with items and set dateTimePickers
            homeIMSRef.assetTypeList typeList = service.getAssetTypes();

            //loop through types
            foreach (homeIMSRef.assetType type in typeList.assetTypes)
            {
                Model.ComboboxItem cboxItem = new Model.ComboboxItem();
                //set values
                cboxItem.Text = type.type;
                cboxItem.Value = type.typeID.ToString();

                //add it to comboBox
                cbAtype.Items.Add(cboxItem);
            }

            //use same list to get Quality
            homeIMSRef.assetQualityList qualityList = service.getQualityList();

            //loop through quality
            foreach (homeIMSRef.assetQuality quality in qualityList.assetQualities)
            {
                Model.ComboboxItem cboxItem = new Model.ComboboxItem();
                //set values
                cboxItem.Text = quality.quality;
                cboxItem.Value = quality.qualityID.ToString();

                //add it to comboBox
                cbAquality.Items.Add(cboxItem);
            }

            //start filling asset information using selected asset

            //GENERIC INFORMATION
            txtAcomments.Text = asset.Comments;
            txtAIDnum.Text = asset.IDNumber;
            txtAmanu.Text = asset.Manufacturer;
            txtAmake.Text = asset.Make;
            txtAmodel.Text = asset.Model;
            txtAname.Text = asset.Name;
            txtAretailVal.Text = asset.RetailValue.ToString();

            //set combo boxes to correct values
            for (int i = 0; i < cbAtype.Items.Count; i++)
            {
                //set variable
                Model.ComboboxItem item = cbAtype.Items[i] as Model.ComboboxItem;
                //check item.value with asset.typeID
                if (asset.Type.ToString() == item.Text.ToString())
                {
                    //if same, make type the text of comboBox and exit
                    cbAtype.Text = item.ToString();
                    break;
                }
                
            }
            for (int i = 0; i < cbAquality.Items.Count; i++)
            {
                //set variable
                Model.ComboboxItem item = cbAquality.Items[i] as Model.ComboboxItem;
                //check item.value with asset.typeID
                if (asset.Quality.ToString() == item.Text.ToString())
                {
                    //if same, make type the text of comboBox and exit
                    cbAquality.Text = item.ToString();
                    break;
                }
            }

            //PURCHASE INFORMATION
            
            //check to see if empty
            if (asset.PurchaseInfo != null)
            {
                //if not empty, fill with information
                dtpPdate.Text = asset.PurchaseInfo.Date.ToShortDateString();
                txtPcomments.Text = asset.PurchaseInfo.Comments;
                txtPcost.Text = asset.PurchaseInfo.Cost.ToString();
                txtPstore.Text = asset.PurchaseInfo.Store;
            }
            
            //WARRANTY INFORMATION

            //check to see if empty

            if (asset.WarrantyInfo != null)
            {
                //walk through each warranty and add it as item in list view
                foreach (Model.warrantyInfo warranty in asset.WarrantyInfo)
                {
                    //create new listViewItem
                    ListViewItem item = new ListViewItem();

                    //add warranty information in order: Start Date, End Date, coverage, phone, comments
                    item.Text = warranty.StartDate.ToShortDateString();
                    item.SubItems[0].Text = warranty.EndDate.ToShortDateString();
                    item.SubItems[1].Text = warranty.Coverage.ToString();
                    item.SubItems[2].Text = warranty.PhoneNum.ToString();
                    item.SubItems[3].Text = warranty.Comment.ToString();
                    
                }
            }

            //MAINTENANCE INFORMATION

            //check to see if empty
            if (asset.maintInfo != null)
            {
                foreach (Model.maintenanceInfo maint in asset.maintInfo)
                {
                    //create new listViewItem
                    ListViewItem item = new ListViewItem();

                    //fill object in order: Date, Location, Company, Price, Comments
                    item.Text = maint.Date.ToShortDateString();
                    item.SubItems[0].Text = maint.Location.ToString();
                    item.SubItems[1].Text = maint.Company.ToString();
                    item.SubItems[2].Text = maint.Price.ToString();
                    item.SubItems[3].Text = maint.Comment.ToString();

                }
            }
        }
    }
}
